package com.kuizii.demo.web.controller;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.kuizii.base.domain.BaseResponse;
import com.kuizii.demo.api.SysUserService;
import com.kuizii.demo.api.UserService;
import com.kuizii.demo.domain.SysUserDto;
import com.kuizii.demo.domain.user.AddUserRequest;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * 首页控制器
 *
 * @author Nuanyang
 * @ClassName: IndexController
 * @date 2016年12月8日 下午8:42:40
 */
@RestController
@RequestMapping("/user")
public class IndexController {

    @Autowired
    SysUserService sysUserService;

    @DubboReference(version = "1.0", group = "demo")
    UserService userService;

    /**
     * 通过feign进行调用 ，本质HTTP协议，较慢，但互通性较强
     *
     * @param response
     * @return
     */
    @GetMapping({"sayHelloByFeign"})
    public BaseResponse<String> sayHelloByFeign(HttpServletResponse response) {
        final SysUserDto user = sysUserService.getUserById("1513710097774526466");
        return BaseResponse.ok(user);
    }

    /**
     * 通过dubbo进行调用 ，支持多协议，默认TCP协议，快，但跨语言互通性较差
     *
     * @param response
     * @return
     */
    @GetMapping({"sayHelloByDubbo"})
    public BaseResponse<String> sayHelloByDubbo(HttpServletResponse response) {
        final SysUserDto user = userService.getUserById("1513710097774526466");
        return BaseResponse.ok(user);
    }


    @PostMapping({"addUser"})
    public BaseResponse<String> addUser(@RequestBody AddUserRequest request, HttpServletResponse response) {
        sysUserService.addUser(request);
        return BaseResponse.ok();
    }

    @ApiOperation("模拟积分变更")
    @PostMapping("addScore")
    public BaseResponse addScore(String userId, BigDecimal score) throws Exception {
        String s = userService.addScore(userId, score);
        return BaseResponse.ok(s);
    }

    @ApiOperation("生成二维码")
    @GetMapping("/qrCode")
    public void qrCode(String content, Integer w, Integer h, HttpServletResponse response) throws Exception {

        int BLACK = 0xFF000000;
        int WHITE = 0xFFFFFFFF;

        if (Objects.isNull(w) || w < 0) {
            w = 400;
        }
        if (Objects.isNull(h) || w < 0) {
            h = 400;
        }

        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, w, h);

        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        ImageIO.write(image, "jpg", response.getOutputStream());
        response.setContentType("image/png");
    }

    @GetMapping("/login")
    public String login() {
        return "no permission";
    }
}
